//
//  LoginAction.m
//  HB
//
//  Created by 孙智慧 on 16/1/27.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "LoginAction.h"
#import "AppDelegate.h"
#import "LoginOperation.h"
#import "RegistSetupOne.h"
#import "ForgetPwd.h"
#import "UserInfo.h"

@implementation LoginAction

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSqliteDB];
    
    //圆角
    self.loginBtn.layer.cornerRadius=2.0;
    
    self.registBtn.layer.cornerRadius=2.0;
    
}

//初始化 sqlite 数据库
-(void) initSqliteDB
{
    db=[DBHelper sharedMyClass];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //显示导航栏
    [super.navigationController setNavigationBarHidden:NO animated:TRUE];
    //[super.navigationController setToolbarHidden:NO animated:TRUE];
    
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    locked=NO;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"1"])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSString *djID=[data1 objectForKey:@"djID"];
            
            [Common setUserDefaults:@"djID" andValue:djID];
            
            UserInfo *ui=[[UserInfo alloc]init];
            
            ui.djID=[data1 objectForKey:@"djID"];
            
            ui.nickName=[data1 objectForKey:@"NickName"];
            
            ui.headImgUrl=[data1 objectForKey:@"HeadImg"];
            
            ui.headImgName =[data1 objectForKey:@"HeadImgName"];
            
            ui.login = YES;
            
            NSDate *date=[NSDate date];
            
            ui.loginDate= date ;
            
            NSString *code=[data1 objectForKey:@"Code"];
            
            ui.code=[code intValue];
            
            NSString *vipID=[data1 objectForKey:@"VipID"];
            
            ui.VipID = [vipID integerValue];
            
            [Common setUserDefaults:@"VipID" andValue:vipID];
            
            ui.VipExpiredTime= [Common convertDateFromString:[data1 objectForKey:@"VipExpiredTime"]];
            
            ui.QualityID= [[data1 objectForKey:@"QualityID"] intValue];
            
            //保存 用户信息
            [db addData:ui];
            
            //登陆成功后显示主页
            [self hideLanch:YES];
        });
    }
    else
    {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [Common showMsg:@"账户或密码错误。请重新登陆" andCancel:YES andHost:self];
        });
        
    }
    
}

- (IBAction)btnLoginClick:(id)sender
{
    
    NSString *uName= self.txtUName.text;
    
    NSString *uPass= self.txtUPass.text;
    
    if(uName==nil || [uName isEqualToString:@""])
    {
        [UIView showAlertView:@"提示" andMessage:@"用户名不能为空！" andHost:self];
    }
    else if(uPass==nil || [uPass isEqualToString:@""])
    {
        [Common showMsg:@"登陆密码不能为空！" andCancel:YES andHost:self];
    }
    else
    {
        [self nextSteup];
    }
}

-(void) nextSteup
{
    [self waitingFor:@"正在登陆" andMethod:@selector(myTask) onTarget:self];
}

- (void)myTask
{
    operation=[[LoginOperation alloc] initWithDelegate:self];
    
    locked=YES;
    
    NSString *uName= self.txtUName.text;
    
    NSString *uPass= self.txtUPass.text;
    
    NSString *urlS= [FxGlobal getServiceUrl:@"user" andFuncName:@"login"];
    
    NSString *url= [NSString stringWithFormat:@"%@?uName=%@&upwd=%@",urlS,uName,uPass];
    
    NSDictionary *dictInfo = @{@"url":url};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
    
    while (locked)
    {
        
    }
}

- (IBAction)btnMobileRegist:(id)sender
{
    RegistSetupOne *registSetupOne=[[RegistSetupOne alloc] init];
    
    [self.navigationController pushViewController:registSetupOne animated:true];
}

//设置状态栏颜色 在Nav控件中不起作用
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}

- (IBAction)btnForgetPwdClick:(id)sender {
    ForgetPwd *forgetPwd =[[ForgetPwd alloc]init];
    [self.navigationController pushViewController:forgetPwd animated:true];
}

@end
